//
//  CCImageEasyGetter.m
//  CCCodeBank
//
//  Created by 成才 向 on 15/10/19.
//  Copyright © 2015年 成才 向. All rights reserved.
//

#import "CCImageGetter.h"
#import <AVFoundation/AVFoundation.h>
#import <AssetsLibrary/AssetsLibrary.h>

@interface CCImageGetter ()
<UIActionSheetDelegate,
UINavigationControllerDelegate,
UIImagePickerControllerDelegate
>
@end

@implementation CCImageGetter

+ (instancetype)sharedImageGetter
{
    static CCImageGetter *__getter = nil;
    if (!__getter)
    {
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            __getter = [[CCImageGetter alloc] init];
        });
    }
    return __getter;
}

- (void)getImageInView:(UIView *)view callback:(void (^)(UIImage *))callback
{
    if (callback)
    {
        self.callback = callback;
    }
    UIActionSheet *act = [[UIActionSheet alloc] initWithTitle:nil
                                                     delegate:self
                                            cancelButtonTitle:@"取消"
                                       destructiveButtonTitle:nil
                                            otherButtonTitles:@"选取照片", @"拍照", nil];
    act.tag = 200;
    [act showInView:view];
}

- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if (buttonIndex != actionSheet.cancelButtonIndex)
    {
        
        if (buttonIndex == 1)
        {
            [self takePhoto];
        }
        else if (buttonIndex == 2)
        {
            [self selectFromAlbum];
        }
    }
}

- (void)takePhoto
{
    //  说明：  当在聊天界面的时候，点击拍照后，相片无法发送出去，并且卡在选择图片界面，程序没报错，但是，只能通过双击home键杀死程序，重新进入。  问题的本质是UIImagePickerController代理中的方法冲突了，导致融云SDK中选择并发送照片的功能失效。解决方法： BlocksKit是开源方法，直接编辑UIImagePickerController+BlocksKit 类文件    将+(void)load里面的代码注释掉即可。但是由于项目中的BlocksKit是通过cocopods导入的，直接修改那个类文件，不能提交新的代码到git。需要每个开发人员自行对其进行修改。并确保项目上线前，做了修改。特此做下说明（此备注说明跟以下代码无关）。
    
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    if (authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
                                                        message:@"请在“设置-隐私-照片”选项中允许特奢汇访问你的相机。"
                                                       delegate:nil
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:nil];
        [alert show];
        return;
    }
    
    if (![UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
                                                        message:@"相机不可用"
                                                       delegate:nil
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:nil];
        [alert show];
        return;
    }
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    [[[[UIApplication sharedApplication] keyWindow] rootViewController] presentViewController:picker animated:YES completion:nil];
}

- (void)selectFromAlbum
{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    [[[[UIApplication sharedApplication] keyWindow] rootViewController] presentViewController:picker animated:YES completion:nil];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    
}

-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [picker dismissViewControllerAnimated:YES completion:nil];
    NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
    
    //当选择的类型是图片
    if ([type isEqualToString:@"public.image"])
    {
        UIImage* image = [info objectForKey:UIImagePickerControllerOriginalImage];
        if (self.callback)
        {
            self.callback(image);
        }
    }
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YES completion:nil];
}

@end
